package com.giaothong.view;

import java.util.Collection;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.giaothong.Main;
import com.giaothong.R;
import com.giaothong.entities.Choice;
import com.giaothong.entities.Question;

public class QuestionView extends LinearLayout {

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	Question question;
	TextView questionTitle, questionImage;
	private Boolean isShowCorrect = Main.getSettingDAL().getSetting().getHideIsCorrect();
	LinearLayout.LayoutParams pl = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT);

	public QuestionView(Context context, Question question, boolean showCorrect){
		super(context);
		this.setQuestion(question);
		this.setOrientation(VERTICAL);
		this.setLayoutParams(pl);
		
		questionTitle = new TextView(context);
		questionImage = new TextView(context);
		
		questionTitle.setTextColor(Color.BLACK);
		
		questionTitle.setText(question.getTitle().trim());
		
		int idPatchQuestion = getResources().getIdentifier(question.getImage().trim(), "drawable", context.getPackageName());
		if(idPatchQuestion > 0){
			questionImage.setBackgroundDrawable(getResources().getDrawable(idPatchQuestion));
		}
		
		this.addView(questionTitle);
		this.addView(questionImage);
		
		Collection<Choice> choiceList = Main.getChoiceDAL().getChoicesByQuestion(question.getId());
		RadioGroup groupRadio = new RadioGroup(context);
		groupRadio.setId(2012);
		if (showCorrect){
			int id = 1;
			// hien thi cac radiobutton
			for (Choice choice : choiceList) {
				RadioButton radio = new RadioButton(context);
				radio.setTextSize(14);
				//radio.setButtonDrawable(R.drawable.customradio);
				radio.setId(id++); 
				radio.setTag(choice);
				radio.setText(Html.fromHtml(choice.getTitle().trim()));
				if(isShowCorrect == true && choice.getIsCorrect() == 1){
					radio.setTextColor(Color.GREEN);
					radio.setChecked(true);}
				else
					radio.setTextColor(Color.BLACK);
				groupRadio.addView(radio);
			}
		} else {	
			int id = 1;
			// hien thi cac radiobutton
			for (Choice choice : choiceList) {
				RadioButton radio = new RadioButton(context);
				radio.setTextSize(14);
				//radio.setButtonDrawable(R.drawable.customradio);
				radio.setId(id++); 
				radio.setTag(choice);
				radio.setText(Html.fromHtml(choice.getTitle().trim()));
				radio.setTextColor(Color.BLACK);
				groupRadio.addView(radio);
			}
		}
		// add RadioGroup to View
		this.addView(groupRadio);
		if (choiceList != null) {
			choiceList.clear();
			choiceList = null;
		}
	}

}
